package pl.koszolko.documentclustering.cluster.document;

import pl.koszolko.documentclustering.cluster.TextVector;
import pl.koszolko.documentclustering.cluster.VectorCreator;
import pl.koszolko.documentclustering.dao.Document;

public class DocumentClusteringByAbstractText extends DocumentForClustering {
	private static final long serialVersionUID = 1L;
	
	private TextVector abstractTextVector;
	
	public DocumentClusteringByAbstractText(Document document) {
		super(document);
		createAbstractTextVector();
		clearAbstractText();
	}
	
	private void createAbstractTextVector() {
		abstractTextVector = VectorCreator.createVectorRepresentation(getAbstractText());
	}

	private void clearAbstractText() {
		document.setAbstractText(null);
	}

	private String getAbstractText() {
		String abstractText = document.getAbstractText();
		if(abstractText == null)
			abstractText = "";
		
		return abstractText;
	}

	@Override
	public double countSimilarity(DocumentForClustering document) {
		if(document instanceof DocumentClusteringByAbstractText)
			return countSimilarityByAbstract((DocumentClusteringByAbstractText)document);
		throw new RuntimeException("Niepoprawny typ DocumentForClustering");
	}
	
	private double countSimilarityByAbstract(DocumentClusteringByAbstractText documet) {
		return abstractTextVector.countSimilarity(documet.abstractTextVector);
	}

	public void setAbstractTextVector(TextVector abstractTextVector) {
		this.abstractTextVector = abstractTextVector;
	}

	public TextVector getAbstractTextVector() {
		return abstractTextVector;
	}
}
